
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
	
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>

<%@ page import ="com.easyattendance.services.AddSubjectService" %>
<%@ page import ="com.easyattendance.model.Subject" %>
<%@ page import ="com.easyattendance.model.Teacher" %>
<%@ page import ="com.easyattendance.services.AddTeacherService" %>
<%@page import="com.easyattendance.services.AddClassService"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Teacher - Subject Settings</title>
<!-- <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> -->
<jsp:include page="../includeFiles.jsp" />


<link rel="stylesheet" href="<%=request.getContextPath()%>/views/css/ui/jquery-ui-1.10.4.custom.min.css">
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.core.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.widget.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.accordion.js"></script>
<script>
	/* If want to fix height inner content of accordion - 
		remove 'heightStyle' line (and the above comma too :p ).
	*/
	$(function() {
		$( "#accordion" ).accordion({
			collapsible: true,
			heightStyle: "content",
		});
	});
</script>

</head>

<body class="left-sidebar">

<!-- Session check : user logged in or not/ press back button -->
<jsp:include page="../common/checkSession.jsp" />
<jsp:include page="../common/checkTeacherRole.jsp" />

<% String currentUser = (String) session.getAttribute("currentUser"); 
AddClassService addClassService = new AddClassService();
String ClassId =addClassService.getClassIdByTeacherId(currentUser); %>
<% AddTeacherService addTeacherService = new AddTeacherService();
Teacher teacher_role = addTeacherService.getTeacherByEmailId(currentUser);
%>
	<!-- Wrapper -->
	<div id="wrapper">

		<!-- Content -->
		<div id="content">
			<div id="content-inner">


				<div>
					<!-- ONLY VISIBLE TO CLASS TEACHER ONLY -->
					<form action="<%=request.getContextPath()%>/views/teacher/addSubject.jsp">
						<input type="submit" value="Add Subject" class="button add_button" tabindex="1">
					</form>
				</div>
				
				<span class="success_msg">${subjectUpdated}</span>
				<span class="error_msg">${subjectNotUpdated}</span>
				<span class="success_msg">${subjectDeleted}</span>
				<span class="error_msg">${subjectNotDeleted}</span>
				<span class="error_msg">${invalidChoice}</span>
				
				<div id="accordion" style="clear:both;">
					<% AddSubjectService addSubjectService = new AddSubjectService();	
					List<Subject> subjectList = new ArrayList<Subject>();
					subjectList = addSubjectService.listSubjectsByClassId(ClassId);
					Subject tempSubject = null; 
					int i=0;
				
					Iterator subjectItr = subjectList.iterator();
					
					while(subjectItr.hasNext()) { i++;%>
						
						<h3> <% try {
									tempSubject = (Subject) subjectItr.next();
								} 
								catch(Exception ex){
									ex.printStackTrace();
								}%> 
								
							<%= tempSubject.getSubject_name() %>
						</h3>
						<div>
						<form name="subjectDeleteForm<%=i%>" action="${pageContext.request.contextPath}/AddSubjectServlet" action="post">
								<div>
									<input type="submit" value="" class="delete_button" alt="Delete" onclick="return confirm('Are you sure you want to delete?')">
									<input type="hidden" id="subject_id<%=i%>" name="subject_id<%=i%>" value="<%= tempSubject.getSubject_id() %>" >
									<input type="hidden" id="class_id<%=i%>" name="class_id<%=i%>" value="<%= ClassId %>" >
									<input type="hidden" id="action_type" name="action_type" value="delete">
									<input type="hidden" id="i_value" name="i_value" value=<%=i%>>
								</div>
						</form>
						<div>
							<form name="subjectUpdateForm<%=i%>" action="${pageContext.request.contextPath}/AddSubjectServlet" action="post">
								<div>
									<input type="hidden" id="subject_id<%=i%>" name="subject_id<%=i%>" value="<%= tempSubject.getSubject_id() %>" >
									<input type="hidden" id="class_id<%=i%>" name="class_id<%=i%>" value="<%= ClassId %>" >
									<input type="hidden" id="action_type" name="action_type" value="update">
									<input type="hidden" id="i_value" name="i_value" value=<%=i%>>
								</div>
								
								<div id="dataForm">
									<label>Name:</label>
									<input type="text" id="subject_name<%=i%>" name="subject_name<%=i%>" value="<%= tempSubject.getSubject_name() %>" tabindex="2">
								</div>
								<div id="dataForm">
									<label>Category:</label>
									<input type="text" id="subject_category<%=i%>" name="subject_category<%=i%>" value="<%= tempSubject.getCategory() %>" tabindex="3">
								</div>
								
								<!-- subject teachers -->
							<div id="dataForm">
								<label>Subject Teacher:</label>
								<select name="teacher_select<%=i%>" id="teacher_select<%=i%>" multiple="multiple" size="2" tabindex="4">
									<option value="">Choose Subject Teacher
										<% List<Teacher> teacherList = new ArrayList<Teacher>();
											String sectionEmail = addTeacherService.getSectionEmailIdByTeacherId(currentUser);
											teacherList = addTeacherService.listTeachers(sectionEmail);
											Teacher tempTeacher = null;
											Iterator teacherItr = teacherList.iterator();
								
										while(teacherItr.hasNext()) { %>
											<% tempTeacher = (Teacher) teacherItr.next();
												String teacherName = tempTeacher.getTeacher_name();
												String teacherId = tempTeacher.getTeacher_id(); %>
									<option value=<%= teacherId%>><%= teacherName %>
									<% } %>
								</select>
							</div>	
								<div>
									<input type="submit" value="Save" class="button add_button" tabindex="5">								
								</div>
							</form>
						</div>
						</div>
					<% } %>
				</div>
				
			</div>
		</div>

		<!-- Sidebar -->
		<div id="sidebar">

			<!-- Sidebar Header - Logo & Name -->
			<jsp:include page="../common/sidebarHeader.jsp" />

			<jsp:include page="teacherMenu.jsp" />

			<!-- Sidebar Footer Content -->
			<jsp:include page="../common/sidebarFooter.jsp" />

		</div>

	</div>	
	<script type="text/javascript">
		$(function(){
			$("#subjectSettings").addClass("current_page_item");
		});
	</script>
	
</body>
</html>